#!/usr/bin/python3

import os
import getopt
import sys


def check_ts_exist(m3u8_file: str, ts_path: str):
    ts_file_list = os.listdir(ts_path)
    # print(ts_file_list)
    print("总数：", len(ts_file_list))
    print('----------------------------------------')
    i = 1
    ts_count = 0
    ts_in_m3u8_list = []
    with open(m3u8_file) as f:
        while f.readable():
            line = f.readline()
            if line == '':
                break
            line = line.replace('\n', '')
            if line.endswith(".ts"):
                i += 1
                ts_name = line.split("/")[-1]
                # print(i, ts_name)
                ts_count += 1
                if ts_file_list.count(ts_name) == 0:
                    print(ts_name, "not found")
                if ts_in_m3u8_list.count(ts_name) == 0:
                    ts_in_m3u8_list.append(ts_name)
                else:
                    print('repeat', ts_name)
    print('ts count:', ts_count)


def main():
    m3u8_file = '/data/战狼2/index.m3u8'
    ts_path = '/data/战狼2'

    opts, args = getopt.getopt(sys.argv[1:], "hu:t:")
    for o, a in opts:
        if o == '-h':
            print("python3 check_dytt_film.py -u " + m3u8_file + " -t " + str(ts_path))
            exit()
        if o == '-u':
            m3u8_file = a
        if o == '-t':
            ts_path = a
    print('m3u8 file：' + m3u8_file)
    print('ts path：' + ts_path)
    check_ts_exist(m3u8_file=m3u8_file, ts_path=ts_path)


if __name__ == "__main__":
    main()
